//
//  MapViewController.m
//  WeddingStoreFinder
//
//  Created by Macdev on 11/19/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MapViewController.h"
#import "ZoomLevel.h"
#import "POI.h"
#import "StoreDetailsViewController.h"

@interface MapViewController()

- (void) reloadMap;
- (void) refreshPOIs;
- (void) removeAllPOIs;

@end

@implementation MapViewController
@synthesize mapView;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self name:kNotification_MapViewController_ReloadMap object:nil];
	[mapView release];
	[super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	[self reloadMap];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMap) name:kNotification_MapViewController_ReloadMap object:nil];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
	[self setMapView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
	self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return interfaceOrientation == UIInterfaceOrientationPortrait;
}

#pragma mark - Private methods

- (void) reloadMap {
	[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(47.497604, 19.058533) zoomLevel:12 animated:YES];
	[self refreshPOIs];
}

- (void) refreshPOIs {
	[self removeAllPOIs];
	NSArray *poiList = [POI getPOIsByStatus:kPOIStatus_OK];
	[self.mapView addAnnotations:poiList];
	
}

- (void) removeAllPOIs {
	NSArray *markerArray = mapView.annotations;
	NSMutableArray *poisForRemove = [[NSMutableArray alloc] init];
	for (int i = 0; i < [markerArray count]; i++) {
		id <MKAnnotation> cMarker = [markerArray objectAtIndex:i];
		if ([cMarker isKindOfClass:[POI class]]) {
			[poisForRemove addObject:cMarker];
			
		}
	}
	[mapView removeAnnotations:poisForRemove];
	[poisForRemove release];
}

#pragma mark - MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
	if ([view.annotation isKindOfClass:[POI class]]) {
		POI *selectedPOI = (POI*)view.annotation;
		StoreDetailsViewController *detailViewController = [[StoreDetailsViewController alloc] initWithNibName:@"StoreDetailsViewController" bundle:nil];
		// ...
		// Pass the selected object to the new view controller.
		detailViewController.poi = selectedPOI;
		[self.navigationController pushViewController:detailViewController animated:YES];
		[detailViewController release];
		/*
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ tapped!", selectedPOI.poiName] message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
		[alert show];
		[alert release];
		 */
	}
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
	} else if ([annotation isKindOfClass:[POI class]]) {
		
		MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
											   initWithAnnotation:annotation reuseIdentifier:@""] autorelease];
		pinView.canShowCallout = YES;
		UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
		pinView.rightCalloutAccessoryView = rightButton;
		
		return pinView;
	}
	return nil;
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
	[AppConf setUserLocation:userLocation];
}




@end
